﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using Model.AuthModel;
using IDAL.AuthModel;
namespace SQLServerDAL.AuthModel
{
    /// <summary>
    /// 通用组件，权限模型，数据层，权限
    /// </summary>
    public class AuthDAL : DalBase, IAuth
    {
        public AuthDAL() 
        {
            Schema = "am.";
            TableName = "Auth";
            ViewTableName = "V_Auth";
            Fields = "AuthID, ModuleID, ModuleName, ModuleLink, ModuleMenu, ModuleTypeID, ModuleTypeName, FuncID, FuncName, FuncMenu";
        }

        #region IAuth成员
        /// <summary>
        /// 更新权限
        /// </summary>
        /// <param name="moduleID">基于模块的模块编号</param>
        /// <param name="funcList">功能列表</param>
        /// <returns></returns>
        public bool UpdateAuth(int moduleID, string funcList)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ModuleID",SqlDbType.Int)
                ,new SqlParameter("@FuncList",SqlDbType.VarChar,10)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = moduleID;
            parm[i++].Value = funcList;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "UpdateAuth", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 权限列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<AuthInfo> ListAuth(AuthCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", Fields, Schema + ViewTableName);
            if (condition.ModuleTypeID > 0)
                SqlBuilder.AppendFormat(" AND ModuleTypeID = {0}", condition.ModuleTypeID);
            if (condition.ModuleID > 0)
                SqlBuilder.AppendFormat(" AND ModuleID = {0}", condition.ModuleID);
            if (!string.IsNullOrEmpty(condition.ModuleMenu))
                SqlBuilder.AppendFormat(" AND ModuleMenu = '{0}'", condition.ModuleMenu);
            if (!string.IsNullOrEmpty(condition.FuncMenu))
                SqlBuilder.AppendFormat(" AND FuncMenu = '{0}'", condition.FuncMenu);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0}", condition.OrderBy);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            List<AuthInfo> list = new List<AuthInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    //AuthID, ModuleID, ModuleName, ModuleLink, ModuleMenu, ModuleTypeID, ModuleTypeName, FuncID, FuncName, FuncMenu
                    AuthInfo info = new AuthInfo();
                    int i = 0;
                    info.AuthID = dr.GetInt32(i++);
                    info.Module = new ModuleInfo();
                    info.Module.ModuleID = dr.GetInt32(i++);
                    info.Module.ModuleName = dr.GetString(i++);
                    info.Module.ModuleLink = dr.GetString(i++);
                    info.Module.ModuleMenu = dr.GetString(i++);
                    info.Module.ModuleType = new ModuleTypeInfo();
                    info.Module.ModuleType.ModuleTypeID = dr.GetInt32(i++);
                    info.Module.ModuleType.ModuleTypeName = dr.GetString(i++);
                    info.Func = new FuncInfo();
                    info.Func.FuncID = dr.GetInt32(i++);
                    info.Func.FuncName = dr.GetString(i++);
                    info.Func.FuncMenu = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }
        #endregion
    }
}
